from flask import Flask, request, jsonify
import arrow
import base64

app = Flask(__name__)


@app.route("/", defaults={"path": ""}, methods=["GET", "POST", "PUT", "DELETE"])
@app.route("/<path:path>", methods=["GET", "POST", "PUT", "DELETE"])
def hello_world(path):
    response = jsonify(
        {
            "msg": "Hello, World!" + " at " + arrow.now().format("YYYY-MM-DD HH:mm:ss"),
            "reqeust": {
                "query": str(request.query_string, "utf-8"),
                "path": path,
                "data": str(request.stream.read(), "utf-8"),
                "clientIp": request.headers.get("x-forwarded-for"),
            },
        }
    )

    # 日志打印
    data = b"Hello logs in flask!"
    encoded_data = base64.b64encode(data)
    response.headers['x-fc-log-result'] = encoded_data.decode('utf-8')

    return response


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=9000)
